<?php
namespace Admin\Controller;
use Think\Controller;
class CategoryController extends CommonController {
    public function index(){
    	$cate=M('category')->field('cid,cname,pid')->select();
		$cate=$this->limitless($cate);
		$this->assign('cate',$cate);
    	$this->display('category');
    }
	/*添加子分类*/
	public function son_add(){
		if(IS_POST){
			$db=array(
				'cname'=>I('post.cname'),
				'pid'=>I('post.pid','','intval')
			);
			$data=M('category')->add($db);
			if($data) $this->success('栏目添加成功！');
		}else{
			$this->display('add_cate');
		}
		
	}
	/*添加顶级分类*/
	public function top_cate(){
		if(IS_POST){
			$db=array(
				'cname'=>I('post.cname'),
				'pid'=>0
			);
			$data=M('category')->add($db);
			if($data) $this->success('栏目添加成功！');
		}else{
			$this->display('add_top_cate');
		}
		
	}
	/*编辑栏目*/
	public function edit(){
		if(IS_POST){
			$cid=I('post.cid','','intval');
			$db=array(
				'cname'=>I('post.cname')
			);
			$data=M('category')->where(array('cid'=>$cid))->save($db);
			if($data) $this->success('栏目修改成功！');
		}else{
			$cid=I('get.cid','','intval');
			$data=M('category')->field('cname,cid')->where(array('cid'=>$cid))->find();
			$this->assign('data',$data);
			$this->display('edit_cate');
		}
		
	}
	public function del(){
		$cid=I('get.cid','','intval');
		M('category')->where(array('cid'=>$cid))->delete();
		$this->success('删除成功！');
	}
}